package com.rosh.rpc.core.netty.codec;

import com.rosh.rpc.core.data.RpcRequest;
import com.rosh.rpc.core.utils.ProtostuffUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;

import java.util.List;

/**
 * @Description:  rpc解码器，输入ByteBuf ，输出RpcRequest
 * @Author: rosh
 * @Date: 2022/6/12 22:22
 */
public class RpcRequestDecoder extends MessageToMessageDecoder<ByteBuf> {


    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) {
        int length = byteBuf.readableBytes();
        byte[] bytes = new byte[length];
        byteBuf.readBytes(bytes);
        //反序列化
        RpcRequest request = ProtostuffUtil.deserialize(bytes, RpcRequest.class);
        list.add(request);
    }
}
